package cn.tedu._06vehicle.base.filters;

import cn.tedu._06vehicle.pojo.vo.UserLoginVO;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

@Slf4j
@WebFilter(filterName="loginCheckFilter",urlPatterns={"/v1/vehicle/*"})
public class LoginCheckFilter implements Filter {

    /**
     *过滤器的初始化方法
     */
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.debug("过滤器正在初始化...");
    }

    /** 过滤器的执行方法：核心方法*/
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        log.debug("进入登录状态校验过滤器...");
        HttpServletRequest req=(HttpServletRequest) request;
        HttpServletResponse resq=(HttpServletResponse) response;

        HttpSession session = req.getSession();

        UserLoginVO userVO=(UserLoginVO)session.getAttribute("user");
        if (userVO!=null) {
            filterChain.doFilter(req, resq);
        }else {
            resq.sendRedirect("/login.html");
        }


    }

     /**过滤器的销毁方法*/
    @Override
    public void destroy() {
       log.debug("过滤器的销毁方法执行了...");
    }
}
